package com.example.myapplication.work

import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.WorkRequest
import com.example.myapplication.R
import com.example.myapplication.common.AbstractFragment
import java.util.*

class WorkerFragment : AbstractFragment() {

    private val default_uuid = "1c6549d7-e7c8-4f07-a8a1-cc80e7b8506d"
    private val viewModel: WorkerViewModel by lazy {
        ViewModelProvider(requireActivity()).get(WorkerViewModel::class.java)
    }

    private val workManager : WorkManager by lazy {
        WorkManager.getInstance(requireContext())
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_worker
    }

    override fun initView(contentView: View) {
        super.initView(contentView)
        val from = OneTimeWorkRequest.from(SleepWork::class.java)
        workManager.getWorkInfoByIdLiveData(from.id).observe(
            requireActivity(), {
                Log.e("WorkerFragment" , "getWorkInfoByIdLiveData > $it")
            }
        )
        with(contentView.findViewById<View>(R.id.bt_worker_1)) {
            this.setOnClickListener {
                workManager
                    .enqueue(from)
            }
        }

        with(contentView.findViewById<View>(R.id.bt_worker_2)){
            this.setOnClickListener {
                val workInfoById = workManager.getWorkInfoById(from.id)
                Log.e("WorkerFragment" , "getWorkInfoById ${from.id} > ${workInfoById.get()}")
            }
        }
        with(contentView.findViewById<View>(R.id.bt_worker_3)){
            this.setOnClickListener {
                val workInfoById = workManager.getWorkInfoById(UUID.fromString(default_uuid))
                Log.e("WorkerFragment" , "getWorkInfoById ${default_uuid} > ${workInfoById.get()}")
            }
        }
    }
}